<?php
require_once 'hd/Compile.php';

class View extends Compile
{
	//分配的变量
	private $vars=[];

	//编译目录
	public $compile_dir = 'compile';

	public $template_dir='tpl';
	//模板文件
	private $tpl;
	// private $compileFile;

	//分配数据
	public function assign()
	{
		$args= func_get_args();
		switch (count($args)) {
			case 1:
			//一个数组
				foreach($args[0] as $name=>$value)
				{
					$this->vars[$name]=$value;	
				}
				break;
			// 2个数组
			case 2:
				$this->vars[$args[0]]=$args[1];
				break;
		}
		
		return $this;
	}


	public function display($tpl)
	{	
		$this->tpl = $this->template_dir.'/'.$tpl;
		//编译文件
		$this->compileFile = $this->compile_dir.'/'.md5($this->tpl).'.php';

		//验证编译文件，编译文件失效时重新编译
		if($this->checkCompileFile()){
			// echo 3333;
			//编译文件
			$this->compile();
		}
		//分配变量 
		extract($this->vars);
		// print_r($this->vars);exit;
		//调用编译文件
		require $this->compileFile;
	}

	/**
	 * 验证编译文件
	 * @return [type] [description]
	 */
	private function checkCompileFile()
	{
		return !is_file($this->compileFile) ||filemtime($this->tpl)>filemtime($this->compileFile);
	}

	//编译模板
	private function compile()
	{	//将整个文件读入字符串
		$content = file_get_contents($this->tpl);
		
		$content = $this->compileTpl($content);

		//写入编译文件
		file_put_contents($this->compileFile, $content);
	}

	

	
}







